<?php
	error_reporting(E_ALL || ~E_NOTICE);

	$strSql="";
	$rowid = $_POST["rowid"];
	$staffnum = $_POST["staffnum"];
	$staffname = $_POST["staffname"];
	$cardnum = $_POST["cardnum"];
	$departmentname = $_POST["departmentname"];
	$controlerNum = $_POST["controlerNum"];
	$departmentID = $_POST["departmentID"];
	$gatenum = $_POST["gatenum"];
//	$gatename = $_POST["gatename"];
	$periodofvalitystart = $_POST["periodofvalitystart"];
	$periodofvalityend = $_POST["periodofvalityend"];	
	$staffgender = $_POST["staffgender"];
	$staffage = $_POST["staffage"];	
//	$picfilename = $_POST["imagefile"];
	$picfilename = $_FILES["imagefile"]["name"];
	file_put_contents("test.log", "rowid: ".$rowid." controlerNum: ".$controlerNum." gatenum: ".$gatenum." staffnum: ".$staffnum." staffname: ".$staffname." departmentname: ".$departmentname." departmentID: ".$departmentID." periodofvalitystart: ".$periodofvalitystart." periodofvalityend: ".$periodofvalityend." staffgender: ".$staffgender." staffage: ".$staffage." picfilename: ".$picfilename." LINE: ".__LINE__."\n", FILE_APPEND);
	if(!$staffnum || !$staffname || !$cardnum)
	{
		echo "<script language=\"JavaScript\">alert('输入为空,请重新输入。');</script><br/>";
		echo '{"errorMsg":"输入为空"}';
		return;
	}

	$auth=false;
	$staffsign = "1";
	if($rowid)
	{
		$add = "update";
	}else
	{
		$add = "add";
	}

	if($gatenum==="全部")
	{
		$gatenum = "all";
	}
	if($controlerNum==="全部")
	{
		$controlerNum = "ALL";
	}
	file_put_contents("test.log","add: ".$add." LINE: ".__LINE__."\n", FILE_APPEND);

	$destination_folder = "tupian/".$staffnum."/";
	$pinfo=pathinfo($picfilename);
	$ftype=$pinfo['extension'];  
	$picfilename = $destination_folder.$staffnum.".".$ftype;

	$myconn=dwopendb();
	if($controlerNum==='ALL')
	{					
		$strSql="select distinct controlerNum,lasttime from gatesetting WHERE controlerNum != 'ALL' ORDER BY lasttime DESC;";
		$result1=mysql_query($strSql,$myconn);
//			$result1=mysql_query($strSql);	
		while($dbResult1 = mysql_fetch_array($result1))
		{
//				@file_put_contents("test.log", "\n controlerNum: ".$dbResult1[0]." add: ".$add."\n", FILE_APPEND);
			if(!$auth && $add=="update")
			{
				if($staffsign=="0")
				{
					$strSql="UPDATE staffbasesetting SET staffname='$staffname',cardnum='$cardnum',departmentname='$departmentname',gatenum='$gatenum',departmentID='$departmentID',periodofvalitystart='$periodofvalitystart',periodofvalityend='$periodofvalityend',staffgender='$staffgender',staffage=$staffage,picfilename='$picfilename',controlerNum='$dbResult1[0]',staffnum='$staffnum' WHERE rowid = $rowid;";

//						$msg="{\"dwdevice\":\"".$dbResult1[0]."\",\"trans_id\":\"".uuid()."\",\"cmd_code\":\"DW_UPDATE_USERINFO\",\"idcard\":\"".$cardnum."\",\"userid\":\"".$staffnum."\",\"departmentname\":\"".$departmentname."\",\"name\":\"".$staffname."\",\"gatenumber\":\"".$gatenum."\",\"departmentID\":\"".$departmentID."\",\"periodofvalitystart\":\"".$periodofvalitystart."\",\"periodofvalityend\":\"".$periodofvalityend."\",\"jingruqishi1\":\"".$jingruqishi1."\",\"jingrujieshu1\":\"".$jingrujieshu1."\",\"likaiqishi1\":\"".$likaiqishi1."\",\"likaijieshu1\":\"".$likaijieshu1."\",\"jingruqishi2\":\"".$jingruqishi2."\",\"jingrujieshu2\":\"".$jingrujieshu2."\",\"likaiqishi2\":\"".$likaiqishi2."\",\"likaijieshu2\":\"".$likaijieshu2."\",\"jingruqishi3\":\"".$jingruqishi3."\",\"jingrujieshu3\":\"".$jingrujieshu3."\",\"likaiqishi3\":\"".$likaiqishi3."\",\"likaijieshu3\":\"".$likaijieshu3."\",\"jingruqishi4\":\"".$jingruqishi4."\",\"jingrujieshu4\":\"".$jingrujieshu4."\",\"likaiqishi4\":\"".$likaiqishi4."\",\"likaijieshu4\":\"".$likaijieshu4."\",\"staffgender\":\"".$staffgender."\",\"staffage\":\"".$staffage."\",\"picfilename\":\"".$picfilename."\"}";
//						dwsendtoserver($msg);
				}else
				{
					$strSql="UPDATE staffbasesetting SET signature='$staffsign',staffname='$staffname',cardnum='$cardnum',departmentname='$departmentname',gatenum='$gatenum',departmentID='$departmentID',periodofvalitystart='$periodofvalitystart',periodofvalityend='$periodofvalityend',staffgender='$staffgender',staffage=$staffage,picfilename='$picfilename',controlerNum='$dbResult1[0]',staffnum='$staffnum' WHERE rowid = $rowid;";
//						$msg="{\"dwdevice\":\"".$dbResult1[0]."\",\"trans_id\":\"".uuid()."\",\"cmd_code\":\"DW_UPDATE_USERINFO\",\"idcard\":\"".$cardnum."\",\"userid\":\"".$staffnum."\",\"staffsign\":\"".$staffsign."\",\"departmentname\":\"".$departmentname."\",\"name\":\"".$staffname."\",\"gatenumber\":\"".$gatenum."\",\"dwpassword\":\"".$dwpassword."\",\"dwforcepass\":\"".$dwforcepass."\",\"periodofvalitystart\":\"".$periodofvalitystart."\",\"periodofvalityend\":\"".$periodofvalityend."\",\"entrancevalitystart\":\"".$entrancevalitystart."\",\"entrancevalityend\":\"".$entrancevalityend."\",\"leavevalitystart\":\"".$leavevalitystart."\",\"leavevalityend\":\"".$leavevalityend."\",\"staffage\":\"".$staffage."\",\"picfilename\":\"".$picfilename."\"}";
//						dwsendtoserver($msg);
				}
				file_put_contents("test.log", "\n strSql: ".$strSql." LINE:".__LINE__."\n", FILE_APPEND);					
				$result=mysql_query($strSql,$myconn);
//					$dbResult = mysql_fetch_array($result);
//					mysql_free_result($result);				
//					if((mysql_affected_rows()==0) or (mysql_affected_rows==-1))
				$rvulae = dwsavepicfile($destination_folder,$picfilename,$url,$staffnum);
				if($result && !$rvulae)
				{
//					dwsendtoserver($msg,$myconn11);
					echo "<script language=\"JavaScript\">alert('修改成功。');</script><br/>";
//						@file_put_contents("test.log", "\n 002 strSql: ".$strSql."\n msg: ".$msg."\n", FILE_APPEND);
				}
				else
				{
					echo '{"errorMsg":"修改失败。"}';
					echo "<script language=\"JavaScript\">alert('修改失败。');</script><br/>";			
//						@file_put_contents("test.log", "\n 003 strSql: ".$strSql."\n msg: ".$msg."\n", FILE_APPEND);
				}
				mysql_free_result($result);					
			}else 
			{
				$strSql="select MAX(rowid) from staffbasesetting WHERE staffnum='$staffnum';";				
				$result=mysql_query($strSql,$myconn);
//					$result=mysql_query($strSql);
				$rowsnum = mysql_fetch_array($result);
//				@file_put_contents("test.log", "\n strSql: ".$strSql." LINE:".__LINE__."\n", FILE_APPEND);
				mysql_free_result($result);
				if($rowsnum[0])
				{
					echo "<script language=\"JavaScript\">alert('人员编号重复。');</script><br/>";
					echo '{"errorMsg":"人员编号重复。"}';
//						@file_put_contents("test.log", "\n rowsnum[0]: ".$rowsnum[0]."\n", FILE_APPEND);
				}else
				{
					if($add=="update")
					{
						$strSql="UPDATE staffbasesetting SET signature='$staffsign',staffname='$staffname',cardnum='$cardnum',departmentname='$departmentname',gatenum='$gatenum',departmentID='$departmentID',periodofvalitystart='$periodofvalitystart',periodofvalityend='$periodofvalityend',staffgender='$staffgender',staffage=$staffage,picfilename='$picfilename',controlerNum='$dbResult1[0]',staffnum='$staffnum' WHERE rowid = $rowid;"; 
//							$msg="{\"dwdevice\":\"".$dbResult1[0]."\"trans_id\":\"".uuid()."\",\"cmd_code\":\"DW_UPDATE_USERINFO\",\"idcard\":\"".$cardnum."\",\"userid\":\"".$staffnum."\",\"staffsign\":\"".$staffsign."\",\"departmentname\":\"".$departmentname."\",\"name\":\"".$staffname."\",\"gatenumber\":\"".$gatenum."\",\"dwpassword\":\"".$dwpassword."\",\"dwforcepass\":\""."\",\"periodofvalitystart\":\"".$periodofvalitystart."\",\"periodofvalityend\":\"".$periodofvalityend."\",\"entrancevalitystart\":\"".$entrancevalitystart."\",\"entrancevalityend\":\"".$entrancevalityend."\",\"leavevalitystart\":\"".$leavevalitystart."\",\"leavevalityend\":\"".$leavevalityend."\",\"staffage\":\"".$staffage."\",\"picfilename\":\"".$picfilename."\"}";
//							dwsendtoserver($msg);
					}else
					{
						$strSql="insert into  staffbasesetting (staffnum,staffname,signature,cardnum, departmentname,departmentID,controlerNum,gatenum,staffgender,staffage,picfilename,periodofvalitystart,periodofvalityend) values ('$staffnum','$staffname','$staffsign','$cardnum','$departmentname','$departmentID','$dbResult1[0]','$gatenum','$staffgender',$staffage,'$picfilename','$periodofvalitystart','$periodofvalityend');";
			
//							$msg="{\"dwdevice\":\"".$dbResult1[0]."\"trans_id\":\"".uuid()."\",\"cmd_code\":\"DW_UPDATE_USERINFO\",\"idcard\":\"".$cardnum."\",\"userid\":\"".$staffnum."\",\"staffsign\":\"".$staffsign."\",\"departmentname\":\"".$departmentname."\",\"name\":\"".$staffname."\",\"gatenumber\":\"".$gatenum."\",\"dwpassword\":\"".$dwpassword."\",\"dwforcepass\":\""."\",\"periodofvalitystart\":\"".$periodofvalitystart."\",\"periodofvalityend\":\"".$periodofvalityend."\",\"entrancevalitystart\":\"".$entrancevalitystart."\",\"entrancevalityend\":\"".$entrancevalityend."\",\"leavevalitystart\":\"".$leavevalitystart."\",\"leavevalityend\":\"".$leavevalityend."\",\"staffage\":\"".$staffage."\",\"picfilename\":\"".$picfilename."\"}";
//							dwsendtoserver($msg);
//						$staffnum = (int)$staffnum + 1;
					}
//						dwsendtoserver($msg);
					@file_put_contents("test.log", "\n strSql: ".$strSql." LINE:".__LINE__."\n", FILE_APPEND);
					$result=mysql_query($strSql,$myconn);						
//						$dbResult = mysql_fetch_array($result);
//						mysql_free_result($result);				
//						if($dbResult[0])
					$rvulae = dwsavepicfile($destination_folder,$picfilename,$url,$staffnum);
					if($result && !$rvulae)
					{
//						dwsendtoserver($msg,$myconn);*/
						if($add=="update")
						{
							echo "<script language=\"JavaScript\">alert('授权成功。');</script><br/>";
//								@file_put_contents("test.log", "\n 004 sqlstr: ".$sqlstr."\n msg: ".$msg."\n", FILE_APPEND);
						}else if($add=="add")
						{
							echo "<script language=\"JavaScript\">alert('增加成功。');</script><br/>";
						}
//							@file_put_contents("test.log", "\n 004 strSql: ".$strSql."\n msg: ".$msg."\n", FILE_APPEND);
					}
					else
					{
						if($add=="update")
						{
							echo "<script language=\"JavaScript\">alert('授权失败。');</script><br/>";
							echo '{"errorMsg":"授权失败。"}';
						}else if($add=="add")
						{
							echo "<script language=\"JavaScript\">alert('增加失败。');</script><br/>";
							echo '{"errorMsg":"增加失败。"}';
						}
					}
					mysql_free_result($result);	
				}				
			}
//				@file_put_contents("test.log", "\n 00 controlerNum: ".$dbResult1[0]." 01 controlerNum: ".$dbResult1[1]." add: ".$add."\n", FILE_APPEND);
		}
		mysql_free_result($result1);
	}else
	{
		if(!$auth && $add=="update")
		{
			if($staffsign=="0")
			{
				$strSql="UPDATE staffbasesetting SET staffname='$staffname',cardnum='$cardnum',departmentname='$departmentname',gatenum='$gatenum',departmentID='$departmentID',periodofvalitystart='$periodofvalitystart',periodofvalityend='$periodofvalityend',staffgender='$staffgender',staffage=$staffage,picfilename='$picfilename',controlerNum='$controlerNum',staffnum='$staffnum' WHERE rowid = $rowid;";				
//					$msg="{\"dwdevice\":\"".$controlerNum."\",\"trans_id\":\"".uuid()."\",\"cmd_code\":\"DW_UPDATE_USERINFO\",\"idcard\":\"".$cardnum."\",\"userid\":\"".$staffnum."\",\"departmentname\":\"".$departmentname."\",\"name\":\"".$staffname."\",\"gatenumber\":\"".$gatenum."\",\"dwpassword\":\"".$dwpassword."\",\"dwforcepass\":\"".$dwforcepass."\",\"periodofvalitystart\":\"".$periodofvalitystart."\",\"periodofvalityend\":\"".$periodofvalityend."\",\"entrancevalitystart\":\"".$entrancevalitystart."\",\"entrancevalityend\":\"".$entrancevalityend."\",\"leavevalitystart\":\"".$leavevalitystart."\",\"leavevalityend\":\"".$leavevalityend."\",\"staffage\":\"".$staffage."\",\"picfilename\":\"".$picfilename."\"}";
//					dwsendtoserver($msg);
			}else
			{
				$strSql="UPDATE staffbasesetting SET staffname='$staffname',cardnum='$cardnum',departmentname='$departmentname',gatenum='$gatenum',periodofvalitystart='$periodofvalitystart',periodofvalityend='$periodofvalityend',staffgender='$staffgender',staffage=$staffage,picfilename='$picfilename',controlerNum='$controlerNum',staffnum='$staffnum' WHERE rowid = $rowid;";

//					$strSql="UPDATE staffbasesetting SET signature='$staffsign',synstate= $staffauth,staffname='$staffname',cardnum='$cardnum',departmentname='$departmentname',gatenum='$gatenum',dwpassword='$dwpassword',dwforcepass='$dwforcepass',periodofvalitystart='$periodofvalitystart',periodofvalityend='$periodofvalityend',entrancevalitystart='$entrancevalitystart',entrancevalityend='$entrancevalityend',leavevalitystart='$leavevalitystart',leavevalityend='$leavevalityend',gatename='$gatename',staffgender='$staffgender',staffage=$staffage,picfilename='$picfilename',controlerNum='$controlerNum' WHERE staffnum='$staffnum';";
//					$msg="{\"dwdevice\":\"".$controlerNum."\",\"trans_id\":\"".uuid()."\",\"cmd_code\":\"DW_UPDATE_USERINFO\",\"idcard\":\"".$cardnum."\",\"userid\":\"".$staffnum."\",\"staffsign\":\"".$staffsign."\",\"departmentname\":\"".$departmentname."\",\"name\":\"".$staffname."\",\"gatenumber\":\"".$gatenum."\",\"dwpassword\":\"".$dwpassword."\",\"dwforcepass\":\"".$dwforcepass."\",\"periodofvalitystart\":\"".$periodofvalitystart."\",\"periodofvalityend\":\"".$periodofvalityend."\",\"entrancevalitystart\":\"".$entrancevalitystart."\",\"entrancevalityend\":\"".$entrancevalityend."\",\"leavevalitystart\":\"".$leavevalitystart."\",\"leavevalityend\":\"".$leavevalityend."\",\"staffage\":\"".$staffage."\",\"picfilename\":\"".$picfilename."\"}";
//					dwsendtoserver($msg);
			}
			@file_put_contents("test.log", "\n strSql: ".$strSql." LINE:".__LINE__."\n", FILE_APPEND);				
			$result=mysql_query($strSql);

			$rvulae = dwsavepicfile($destination_folder,$picfilename,$url,$staffnum);
			if($result && !$rvulae)
			{
//				dwsendtoserver($msg,$TCPport);*/
				echo "<script language=\"JavaScript\">alert('修改成功。');</script><br/>";
//					@file_put_contents("test.log", "\n 000 strSql: ".$strSql."\n msg: ".$msg."\n", FILE_APPEND);
			}
			else
			{
				echo "<script language=\"JavaScript\">alert('修改失败。');</script><br/>";
				echo '{"errorMsg":"修改失败"}';
//					@file_put_contents("test.log", "\n 001 strSql: ".$strSql."\n msg: ".$msg."\n", FILE_APPEND);
			}
		}else
		{
			$strSql="select MAX(skid) from staffbasesetting WHERE staffnum='$staffnum';";
			$result=mysql_query($strSql,$myconn);
//				$result=mysql_query($strSql);
			$rowsnum = mysql_fetch_array($result);
			mysql_free_result($result);
			if($rowsnum[0])
			{
				echo "<script language=\"JavaScript\">alert('人员编号重复。');</script><br/>";
				echo '{"errorMsg":"人员编号重复"}';
//					echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='".$url."',5000);</script><br/>";
			}else
			{
				if($add=="update")
				{
					$strSql="UPDATE staffbasesetting SET staffname='$staffname',cardnum='$cardnum',departmentname='$departmentname',gatenum='$gatenum',departmentID='$departmentID',periodofvalitystart='$periodofvalitystart',periodofvalityend='$periodofvalityend',staffgender='$staffgender',staffage=$staffage,picfilename='$picfilename',controlerNum='$controlerNum',staffnum='$staffnum' WHERE rowid = $rowid;";

					@file_put_contents("test.log", "\n strSql: ".$strSql." LINE:".__LINE__."\n", FILE_APPEND);
//						$msg="{\"dwdevice\":\"".$controlerNum."\",\"trans_id\":\"".uuid()."\",\"cmd_code\":\"DW_UPDATE_USERINFO\",\"idcard\":\"".$cardnum."\",\"userid\":\"".$staffnum."\",\"staffsign\":\"".$staffsign."\",\"departmentname\":\"".$departmentname."\",\"name\":\"".$staffname."\",\"gatenumber\":\"".$gatenum."\",\"dwpassword\":\"".$dwpassword."\",\"dwforcepass\":\"".$dwforcepass."\",\"periodofvalitystart\":\"".$periodofvalitystart."\",\"periodofvalityend\":\"".$periodofvalityend."\",\"entrancevalitystart\":\"".$entrancevalitystart."\",\"entrancevalityend\":\"".$entrancevalityend."\",\"leavevalitystart\":\"".$leavevalitystart."\",\"leavevalityend\":\"".$leavevalityend."\",\"staffage\":\"".$staffage."\",\"picfilename\":\"".$picfilename."\"}";						
				}else if($add=="add")
				{
					$strSql="insert into  staffbasesetting (staffnum,staffname,signature,cardnum, departmentname,departmentID,controlerNum,gatenum,staffgender,staffage,picfilename,periodofvalitystart,periodofvalityend) values ('$staffnum','$staffname','$staffsign','$cardnum','$departmentname','$departmentID','$controlerNum','$gatenum','$staffgender',$staffage,'$picfilename','$periodofvalitystart','$periodofvalityend');";
					@file_put_contents("test.log", "\n strSql: ".$strSql." LINE:".__LINE__."\n", FILE_APPEND);
//						$msg="{\"dwdevice\":\"".$controlerNum."\",\"trans_id\":\"".uuid()."\",\"cmd_code\":\"DW_UPDATE_USERINFO\",\"idcard\":\"".$cardnum."\",\"userid\":\"".$staffnum."\",\"staffsign\":\"".$staffsign."\",\"departmentname\":\"".$departmentname."\",\"name\":\"".$staffname."\",\"gatenumber\":\"".$gatenum."\",\"dwpassword\":\"".$dwpassword."\",\"dwforcepass\":\"".$dwforcepass."\",\"periodofvalitystart\":\"".$periodofvalitystart."\",\"periodofvalityend\":\"".$periodofvalityend."\",\"entrancevalitystart\":\"".$entrancevalitystart."\",\"entrancevalityend\":\"".$entrancevalityend."\",\"leavevalitystart\":\"".$leavevalitystart."\",\"leavevalityend\":\"".$leavevalityend."\",\"staffage\":\"".$staffage."\",\"picfilename\":\"".$picfilename."\"}";	
//						@file_put_contents("test.log", "\n msg: ".$msg."\n", FILE_APPEND);		
				}
				$result=mysql_query($strSql,$myconn);

				$rvulae = dwsavepicfile($destination_folder,$picfilename,$url,$staffnum);
				if($result && !$rvulae)
				{
//					dwsendtoserver($msg,$TCPport);*/
					if($add=="update")
					{
						echo "<script language=\"JavaScript\">alert('授权成功。');</script><br/>";
//							@file_put_contents("test.log", "\n 005 strSql: ".$strSql."\n msg: ".$msg."\n", FILE_APPEND);
					}else if($add=="add")
					{
						echo "<script language=\"JavaScript\">alert('增加成功。');</script><br/>";
					}
				}
				else
				{
					if($add=="update")
					{
						echo "<script language=\"JavaScript\">alert('授权失败。');</script><br/>";
						echo '{"errorMsg":"授权失败"}';
					}else if($add=="add")
					{
						echo "<script language=\"JavaScript\">alert('增加失败。');</script><br/>";
						echo '{"errorMsg":"增加失败"}';
					}
				}
			}	
		}
//			mysql_free_result($result);						
	}
	dwclosedb($myconn);
	return 0;

//	echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='".$url."',5000);</script><br/>";

	function dwsavepicfile($destination_folder,$picfilename,$url,$gonghao)
	{
		$max_file_size = 500*1024;
		$uptypes=array(  
		'image/jpg',  
		'image/jpeg',  
		'image/png',  
		'image/pjpeg',  
		'image/gif',  
		'image/bmp',  
		'image/x-png'  
		); 
		$pinfo=pathinfo($picfilename);
		$picfilename = $destination_folder.$pinfo["basename"]; 
		if(!file_exists($destination_folder))
//		if(!is_dir($destination_folder))  
		{  
			mkdir($destination_folder,0766);
			chmod($destination_folder, 0766);
		}

//		if (!is_uploaded_file($_FILES["filename"][tmp_name]))
		if (!is_uploaded_file($_FILES["imagefile"]["tmp_name"]))  
		//是否存在文件  
		{  
			echo "<script language=\"JavaScript\">alert('图片文件不存在。error: ".$_FILES["imagefile"]["error"]."');</script><br/>";
//			echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='".$url."',5000);</script><br/>";
			echo '{"errorMsg":"图片文件不存在"}';
			file_put_contents("test.log", "\n error: ".$_FILES["imagefile"]["error"]." LINE: ".__LINE__."\n", FILE_APPEND);
			return -1;	
		}else 
		{
//			$file = $_FILES["filename"]; 
			$file = $_FILES["imagefile"]; 
			if($file["size"] > $max_file_size)  
			//检查文件大小  
			{  
				echo "<script language=\"JavaScript\">alert('图片文件太大。');</script><br/>";
//				echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='".$url."',5000);</script><br/>";
				echo '{"errorMsg":"图片文件太大。"}';
				return -1;	
			}else
			{		  
				if(!in_array($file["type"], $uptypes))  
				//检查文件类型  
				{  
					echo "<script language=\"JavaScript\">alert('\"图片文件类型不符。\"".$file["type"]."。');</script><br/>";
//					echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='".$url."',5000);</script><br/>";
					echo '{"errorMsg":"图片文件类型不符。"}';
					return -1;	
				}else
				{				  
					$filename=$file["tmp_name"];  
//					$image_size = getimagesize($filename);  
					$pinfo=pathinfo($file["name"]);  
					$real_filename = $pinfo["basename"];
					$ftype=$pinfo['extension'];  
//					$destination = $destination_folder.$real_filename;
					$destination = "/home/dawu/www/class/".$destination_folder.$gonghao.'.'.$ftype;
//					$destination = $destination_folder.$gonghao.'.'.$ftype;
					file_put_contents("test.log", "\n gonghao: ".$gonghao." destination_folder: ".$destination_folder." ftype: ".$ftype." destination: ".$destination." filesize: ".$file["size"]." filename: ".$filename." real_filename: ".$real_filename." LINE: ".__LINE__."\n", FILE_APPEND); 
					if (file_exists($destination) )  
					{  
						echo "<script language=\"JavaScript\">alert('同名图片文件已经存在了。');</script><br/>";
//						echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='".$url."',5000);</script><br/>";
						echo '{"errorMsg":"同名图片文件已经存在了"}';
//						return -1;	
					}  
				  	
					if(!move_uploaded_file ($filename, $destination))  
					{  
						echo "<script language=\"JavaScript\">alert('移动图片文件出错。');</script><br/>";
//						echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='".$url."',5000);</script><br/>";
						echo '{"errorMsg":"移动图片文件出错"}';
						return -1;	
					}else
					{
						$biduilujing="/home/dawu/www/biduilujing/";
						file_put_contents("test.log", "{\"dongzuo\":\"dwquzhi\",\"zaopianquanming\":\"$destination\",\"gonghao\":\"$gonghao\",\"biduilujing\":\"$biduilujing\"}"." LINE: ".__LINE__."\n", FILE_APPEND);
//						$fanhuizhi=file_put_contents("/home/dawu/www/cgi-bin/qidongtingzhi.txt", "{\"dongzuo\":\"dwquzhi\",\"zaopianquanming\":\"$destination\",\"gonghao\":\"$gonghao\",\"biduilujing\":\"$biduilujing\"}");
						$minglingchuan="/home/dawu/www/cgi-bin/dwrlquzhi.sh ".$destination." ".$gonghao." ".$biduilujing." 2>&1";
						exec($minglingchuan, $output, $fanhuizhi);
						$fanhuichuan=implode(" ",$output);
						file_put_contents("test.log",'dwqietu.sh 2>&1:'.$fanhuichuan.' fanhuizhi:'.$fanhuizhi.' minglingchuan '.$minglingchuan." LINE ".__LINE__."\n", FILE_APPEND); 
						$pos = strpos($fanhuichuan,"dwrlquzhi: ok");
						if( $fanhuizhi || $pos === false)
						{			
//							echo '{"errorMsg":"'.$fanhuichuan.'"}';
							echo "<script language=\"JavaScript\">alert('增加图片文件 取特征值出错。');</script><br/>";
//							echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='".$url."',5000);</script><br/>";
							echo '{"errorMsg":"增加图片文件出错 取特征值出错。 '.$fanhuichuan.'"}';
							file_put_contents("test.log", '{"errorMsg":"增加图片文件出错 取特征值出错。"}'." LINE: ".__LINE__."\n", FILE_APPEND);
						}else
						{
//							echo '{"success":"true"}';
							echo "<script language=\"JavaScript\">alert('增加图片文件 取特征值成功。');</script><br/>";
//							echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='".$url."',5000);</script><br/>";
							file_put_contents("test.log", '增加图片文件 取特征值成功。'." LINE: ".__LINE__."\n", FILE_APPEND);
						}
					}
					file_put_contents("test.log", "\n filename: ".$filename." image_size: ".$image_size." ftype: ".$ftype." destination: ".$destination." LINE: ".__LINE__."\n", FILE_APPEND);
				}
			}
		}		
	}

	function dwclosedb($myconn)
	{
		mysql_close($myconn);	
	}
	
	function dwopendb()
	{
		$myconn=@mysql_connect("localhost","dw","DWdawu-309");
		if(!$myconn)
		{
//			die('Could not connect: '.mysql_error());
			$string = "Can't connect database : ".mysql_error()." LINE:".__LINE__."\n";
			file_put_contents("test.log", $string, FILE_APPEND);
			return false;
		}
		$db_selected=mysql_select_db("dwzuapairenlian",$myconn);
		if(!$db_selected)
		{
//			die("Can't use database dwaccessDB : ".mysql_error());       
			$strSql="CREATE DATABASE dwzuapairenlian;";
			$result=mysql_query($strSql,$myconn);
			if($result==false)
			{
				die("Can't use database dwaccessDB : ".mysql_error());
				$string = "Can't use database : ".mysql_error()." LINE:".__LINE__."\n";
				file_put_contents("test.log", $string, FILE_APPEND);
				dwclosedb($myconn);
				$response = base64_encode("{\"res\":\"deny3\"}");
				echo $response;					
				return false;
			}
			$strSql="set character_set_database=utf8;";
			$result=mysql_query($strSql,$myconn);
	
			mysql_close($myconn);
			return false;
		}
		return $myconn;
	}
?>
